﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IsmsExplorer.IsmsDataImport.UI.Common {
    public static class UIHelper {
        static OpenFileDialog _openFileDlg;
        static SaveFileDialog _saveFileDlg;
        static FolderBrowserDialog _folderBrowseDlg;

        static UIHelper() {
            _openFileDlg = new OpenFileDialog();
            _saveFileDlg = new SaveFileDialog();
            _folderBrowseDlg = new FolderBrowserDialog();
        }

        public static string BrowseForFileOpen(string title = "", string filter = "", IWin32Window owner = null) {
            _openFileDlg.CheckFileExists = true;
            _openFileDlg.Filter = string.IsNullOrEmpty(filter) ?
                "Word documents (*.doc, *.docx)|*.doc;*.docx|Excel documents (*.xls, *.xlsx)|*.doc;*.docx|Text files (*.txt)|*.txt|All Files (*.*)|*.*" : filter;
            _openFileDlg.FilterIndex = 1;
            _openFileDlg.Multiselect = false;
            _openFileDlg.RestoreDirectory = true;
            _openFileDlg.Title = title;

            if (_openFileDlg.ShowDialog(owner) == System.Windows.Forms.DialogResult.OK) {
                return _openFileDlg.FileName;
            } else {
                return null;
            }
        }

        public static string BrowseForFileSave(string title = "", string filter = "", IWin32Window owner = null) {
            _saveFileDlg.CheckFileExists = true;
            _saveFileDlg.Filter = string.IsNullOrEmpty(filter) ?
                "Word documents (*.doc, *.docx)|*.doc;*.docx|Excel documents (*.xls, *.xlsx)|*.doc;*.docx|Text files (*.txt)|*.txt|All Files (*.*)|*.*" : filter;
            _saveFileDlg.FilterIndex = 1;
            _saveFileDlg.RestoreDirectory = true;
            _saveFileDlg.Title = title;
            _saveFileDlg.OverwritePrompt = true;
            _saveFileDlg.ValidateNames = true;

            if (_openFileDlg.ShowDialog(owner) == System.Windows.Forms.DialogResult.OK) {
                return _openFileDlg.FileName;
            } else {
                return null;
            }
        }

        public static string BrowseForFolder(string title = "", IWin32Window owner = null) {
            _folderBrowseDlg.Description = title;
            _folderBrowseDlg.ShowNewFolderButton = true;

            if (_folderBrowseDlg.ShowDialog(owner) == System.Windows.Forms.DialogResult.OK) {
                return _folderBrowseDlg.SelectedPath;
            } else {
                return null;
            }
        }

        public static void ShowErrorMessage(IWin32Window owner, string message, string title = "") {
            MessageBox.Show(owner, message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static void ShowInfoMessage(IWin32Window owner, string message, string title = "") {
            MessageBox.Show(owner, message, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static DialogResult ShowYesNoQuestion(IWin32Window owner, string message, MessageBoxDefaultButton defaultButton, string title = "") {
            return MessageBox.Show(owner, message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, defaultButton);
        }

    }
}
